package common.store.admin.contoller;

import common.store.admin.dto.Category;
import common.store.admin.dto.Product;
import common.store.admin.service.AdminProductService;
import common.store.admin.service.CategoryService;
import common.store.admin.service.impl.AdminProductServiceImpl;
import common.store.admin.service.impl.CategoryServiceImpl;
import common.store.admin.vo.QueryVo;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/adminProductList")
public class AdminProductListServlet extends HttpServlet {

    private CategoryService categoryService;
    private AdminProductService adminProductService;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
       categoryService =new CategoryServiceImpl();
        List<Category> categories=categoryService.getCategoryList();
       adminProductService =new AdminProductServiceImpl();
        QueryVo qv=new QueryVo();
        try {
            BeanUtils.populate(qv,req.getParameterMap());
            List<Product> products=adminProductService.getProductList(qv);
            req.setAttribute("productList" , products);
            req.setAttribute("qv" , qv);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        req.setAttribute("categoryList" , categories);


        req.getRequestDispatcher("/admin/product/list.jsp").forward(req , resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
